<template>
  <view class="container" :style="appThemeStyle">
    <mescroll-body
      ref="mescrollRef"
      :sticky="true"
      @init="mescrollInit"
      :down="{ native: true }"
      :up="upOption"
      @up="upCallback"
      @down="downCallback"
    >
      <order-list
        :list="list"
        @controls="handleControls"
      />
    </mescroll-body>

    <!--部分退限制退款提示-->
    <u-modal
      v-model="showPartModal"
      title="提示"
      :content="partModalText"
      confirm-text="我已知悉"
      :title-style="{
        fontWeight: 'bold'
      }"
      @confirm="showPartModal = false"
    >
    </u-modal>

    <!-- 消费返券确认弹窗 -->
    <RefundCoupon
      :list="refundCouponList"
      ref="refundCouponRef"
      @refundModal="refundModalConfirm"
    />

    <!-- 打印确定弹窗 -->
    <PrintPopup
      ref="printPopupRef"
      @confirm="confirmPrint"
    />

    <!-- 查询弹窗 -->
    <SearchPopup
      ref="searchRef"
      @search="searchForm"
    />

    <!--部分退提交弹窗-->
    <PartPopup
      ref="partPopupRef"
      :order="confirmOrder"
      @confirm="handlePartConfirm"
    />

    <!-- 骑手返货 -->
    <ReturnGoods :order="confirmOrder" ref="returnGoodsRef"/>

    <!--订单轨迹-->
    <Tracks :list="tracks" ref="tracksRef"/>
  </view>
</template>

<script>

import OrderList from './components/order-list.vue'
import Tracks from './components/tracks.vue'
import OrderMixin from '@/core/mixins/order'
import SearchPopup from './components/search-popup.vue'
import PartPopup from './components/part-popup.vue'
import ReturnGoods from './components/return-goods.vue'
import PrintPopup from './components/print-popup.vue'
import RefundCoupon from './components/refund-coupon.vue'
import { cloneObj } from "@/utils/util"
import * as OrderApi from "@/api/order/order"
import * as RefundApi from "@/api/order/refund"
import MescrollMixin from '@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins'
import { getEmptyPaginateObj, getMoreListData } from '@/core/app'
import store from "@/store"

export default {
  name: "search",
  components: {
    SearchPopup,
    OrderList,
    Tracks,
    RefundCoupon,
    PartPopup,
    ReturnGoods,
    PrintPopup,
  },
  mixins: [MescrollMixin, OrderMixin],
  data() {
    return {
      // 上拉加载配置
      upOption: {
        // 首次自动执行
        auto: true,
        // 每页数据的数量; 默认10
        page: { size: 10 },
        // 数量要大于3条才显示无更多数据
        noMoreSize: 3,
        empty: {
          tip: '这里空空如也~'
        }
      },
      list: getEmptyPaginateObj(),
      queryParams: {
        startTime: this.$moment().add(-6, 'days').format("YYYY-MM-DD 00:00:00"),
        endTime: this.$moment().format("YYYY-MM-DD 23:59:59")
      }
    }
  },
  computed: {
    businessType() {
      return store.getters.businessType
    },
    store() {
      return store.getters.store
    },
  },
  methods: {
    upCallback(page) {
      this.getOrderList(page.num)
        .then(res => {
          const curPageLen = res.length
          this.mescroll.endBySize(curPageLen)
        })
        .catch(() => this.mescroll.endErr())
    },
    getOrderList(current = 1) {
      if (this.businessType !== 0) {
        this.queryParams.organizationIds = this.store.id
      } else {
        delete this.queryParams.organizationIds
      }
      const nParams = cloneObj(this.queryParams)
      delete nParams.orderType
      const fetch = !this.queryParams.orderType ? OrderApi.getOrderList : RefundApi.getRefundList
      return new Promise((resolve, reject) => {
        fetch({ ...nParams, current }, { load: false })
          .then(result => {
            const newList = result.data
            this.list = getMoreListData(newList, this.list, current)
            resolve(newList)
          })
          .catch(reject)
      })
    },
    searchForm(values) {
      this.queryParams = { ...this.queryParams, ...values }
      this.onRefreshList()
    },
    // 刷新列表
    onRefreshList() {
      this.list = getEmptyPaginateObj()
      setTimeout(() => this.mescroll.resetUpScroll(), 120)
    },
  }
}
</script>

<style scoped lang="scss">
.container {
  padding: 24rpx;
}
</style>
